var dataDone = [];
var dataUndone = [];
var selectDateData = true;
var selectDataByDate = true;

$(document).ready(function () {
    selectDate("startDate");
    selectDate("endDate");
    search();
});
function doOpenDate(OneElement, TwoElement, ThreeElement) {
    $("#" + ThreeElement).hide();
    $('#' + OneElement).show();
    $('#' + TwoElement).show();
}
function doGetDateNavigations(txtBeginDate) {
    var dateNavigations = $("#dateNavigations");
    dateNavigations.empty();
    $.post("doGetDateNavigations", {"txtBeginDate": txtBeginDate}, function (result) {
        var list = result.list;
        var month = result.month;
        var week = result.week;
        if (list.length > 0) {
            dateNavigations.append('<ul class="nav navbar-nav topTitle" ></ul>');
            $("#newTimeMsg").html((txtBeginDate.getMonth() + 1) + "月 第" + week + "周");
            for (var i = 0; i < list.length; i++) {
                var $formatDate = new Date(list[i]);
                $formatDate = ($formatDate.getMonth() + 1) + "月" + $formatDate.getDate() + "日";
                dateNavigations.children().append('<li class="listInformation commonBlueFont" id="listInformation'+ i +'" onMouseOver="mmigration(\'listInformation'+ i +'\')\" onMouseOut=\"shiftOut()\"' +
                    ' onclick="searchLog(\'' + list[i] + '\',\'\', \'listInformation'+ i +'\')">' + $formatDate + '</li>');
                if (i == list.length - 1) {
                    dateNavigations.children().append('<li class="listInformation commonBlueFont" id="listInformationLast"  onMouseOver="mmigration(\'listInformationLast\')\" onMouseOut=\"shiftOut()\"' +
                        ' onclick="searchLog(\'' + list[0] + '\',\'' + list[list.length - 1] + '\',\'listInformationLast\' )">周报</li>');
                }
            }
            dateNavigations.append('<input type="button" onclick="exportReport()" value="导出" id="Export"/>');
        } else {
            $("#newTimeMsg").html('');
            showMessage("创建日期导航失败");
        }
        if (selectDataByDate) {
            var listInformation = $(".listInformation");
            for (var i = 0; i < listInformation.length; i++) {
                if (listInformation[i].innerHTML == timeStamp2String(txtBeginDate,3)) {
                    $(listInformation[i]).click();
                }
            }
            selectDataByDate = false;
        }
    });
}
function search() {
    if (selectDateData == true) {
        doGetDateNavigations(stringToDate(timeStamp2String(new Date(), 2)));
        $("#txtBeginDate").val(timeStamp2String(new Date(), 2));
        selectDateData = false;
        return;
    }

    if ($("#txtBeginDate").val() != '') {
        var txtBeginDate = stringToDate($("#txtBeginDate").val());
        doGetDateNavigations(txtBeginDate);
    } else {
        $("#newTimeMsg").html('');
        showMessage("请先选择时间,否则无法查询");
    }
    $('.activeSpan').removeClass("activeSpan");
    $("#completeTbody").html('');
    $("#noCompleteTbody").html('');
}
function doGetAllMembers(OneElement,TwoElement,ThreeElement){
    $.post("doGetAllMembers",function (result) {
        if(result.state==1){
            var list=result.data;
            var backLogMembers=$("#backLogMembers");
            backLogMembers.empty();
            for(var i in list){
                var memberInitials=list[i].userName.substring(0,1);
                backLogMembers.append('<div class="memberList" id="'+list[i].userID+'">'+
                    '<span class="memberLabel">'+memberInitials+'</span>'+
                    '<span id="'+list[i].userID+'span" class="memberName commonBlueFont"  onMouseOver="mmigration(\''+ list[i].userID +'span\')" onMouseOut="shiftOut()" onclick="getBackLogByUser(\''+ list[i].userID +'\')">'+list[i].userName+'</span></div>');
            }
            $("#"+ThreeElement).hide();
            $('#'+OneElement).show();
            $('#'+TwoElement).show();
        }else{
            showMessage("获取成员失败");
        }
        setUserNameStyle();
    });
}

function exportReport() {
    if (dataDone.length == 0 && dataUndone.length == 0) {
        showMessage("没有数据可导出!");
    } else {
        var txtBeginDate = $("#txtBeginDate").val();
        document.getElementById("ifile").src = "exportReport?txtBeginDate=" + txtBeginDate;
    }
}

function searchLog(dataOne, dataTwo, ID) {
    $(".listInformation").removeClass("fontColorBlue");
    $("#" + ID).addClass("fontColorBlue");
    var timeOne = stringToDate(dataOne);
    var timeTwo = undefined;
    if (dataTwo == '') {
        timeTwo = stringToDate(dataOne);
    } else {
        timeTwo = stringToDate(dataTwo);
    }
    if (timeOne != '' && timeTwo != '') {
        $.post("doGetBackLogMsgDetalis", {
            "ActualStartDTTM": timeOne,
            "ActualEndDTTM": timeTwo,
            "UserID":getMemberId()
        }, function (result) {
            dataOne = result && result.listOne || [];
            dataTwo = result && result.listTwo || [];
            dataDone = result && result.listOne || [];
            dataUndone = result && result.listTwo || [];
            $("#completeTbody").html('');
            $("#noCompleteTbody").html('');
            var completeTbody = $("#completeTbody");
            var noCompleteTbody = $("#noCompleteTbody");
            if (dataOne.length == 0 && dataTwo.length == 0) {
                showMessage("没有查询数据");
            } else {
                if (dataOne.length > 0) {
                    for (var i = 0; i < dataOne.length; i++) {
                        var data = dataOne[i];
                        var $tr = $('<tr>').appendTo(completeTbody);
                        $("<td>").html(data.productName || "未知产品").appendTo($tr);
                        $("<td>").html(data.productVersionName || "未知版本").appendTo($tr);
                        $("<td>").html(data.moduleName || "未知功能").appendTo($tr);
                        $("<td>").html(data.backLogName || "未知Log").appendTo($tr);
                        $("<td>").html(data.expectedWorkDate + "人工").appendTo($tr);
                        $("<td>").html(data.stateName || "未知状态").appendTo($tr);
                        $("<td>").html(data.userName || "未知人").appendTo($tr);
                    }
                }
                if (dataTwo.length > 0) {
                    for (var i = 0; i < dataTwo.length; i++) {
                        var dataTwoMsg = '';
                        var data = dataTwo[i];
                        var $tr = $('<tr>').appendTo(noCompleteTbody);
                        $("<td>").html(data.productName || "未知产品").appendTo($tr);
                        $("<td>").html(data.productVersionName || "未知版本").appendTo($tr);
                        $("<td>").html(data.moduleName || "未知功能").appendTo($tr);
                        $("<td>").html(data.backLogName || "未知Log").appendTo($tr);
                        $("<td>").html(data.expectedWorkDate + "人工").appendTo($tr);
                        $("<td>").html(data.stateName || "未知状态").appendTo($tr);
                        $("<td>").html(data.userName || "未知人").appendTo($tr);
                    }
                }
            }
        });
    } else {
        showMessage("没有获取到相应数据,无法查询");
    }

}
function getMemberId() {
    var $MemberId = '';
    var MemberId = $(".memberList").children("span:last-child");
    for (var i = 0; i < MemberId.length; i++) {
        if (MemberId[i].className.indexOf("activeSpan") >= 0) {
            $MemberId = MemberId[i].id.replace("span", "");
        }
    }
    return $MemberId;
}
function getBackLogByUser($MemberID) {
    $('.activeLi').removeClass("activeLi");
    $('.activeSpan').removeClass("activeSpan");
    $("#"+$MemberID+"span").addClass("activeSpan");
    var selectDate = $(".listInformation");
    for (var i = 0; i < selectDate.length; i++) {
        if (selectDate[i].className.indexOf("fontColorBlue") >= 0) {
            $(selectDate[i]).click();
        }
    }
}
